package com.nathan.crm.controller;

import com.nathan.base.BaseController;
import com.nathan.base.ResultInfo;
import com.nathan.crm.query.CustomerReprieveQuery;
import com.nathan.crm.service.CustomerReprieveService;
import com.nathan.crm.vo.CustomerReprieve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("customer_rep")
public class CustomerReprieveController extends BaseController {
    @Autowired
    private CustomerReprieveService customerReprieveService;

    /**
     * 根据流失客户id获取暂缓数据
     * @param customerReprieveQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(CustomerReprieveQuery customerReprieveQuery){
        return customerReprieveService.queryByParamsForTable(customerReprieveQuery);
    }

    /**
     * 添加暂缓数据
     * @param customerReprieve
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addCustomerRep(CustomerReprieve customerReprieve){
        customerReprieveService.addCustomerRep(customerReprieve);
        return success("暂缓措施添加成功！");
    }

    /**
     * 修改暂缓数据
     * @param customerReprieve
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateCustomerRep(CustomerReprieve customerReprieve){
        customerReprieveService.updateCustomerRep(customerReprieve);
        return success("暂缓措施修改成功！");
    }

    /**
     * 根据id删除暂缓措施数据
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomerRep(Integer id){
        customerReprieveService.deleteCustomerRepById(id);
        return success("删除成功！");
    }

    /**
     * 打开添加或修改暂缓数据页面
     *
     * @param lossId    流失客户id
     * @param id    暂缓数据id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateCustomerRepPage")
    public String addOrUpdateCustomerRepPage(Integer lossId, Integer id, Model model){
        if (id != null){
            CustomerReprieve customerReprieve = customerReprieveService.selectByPrimaryKey(id);

            model.addAttribute("customerRep",customerReprieve);
        }

        model.addAttribute("lossId",lossId);
        return "customerLoss/customer_rep_add_update";
    }
}
